/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Responses;

import Utilities.ApplicationException;
import java.io.Serializable;
import Utilities.Interfaces.SelfValidating;
import java.util.ArrayList;

/**
 * This is the superclass for all responses being passed between the components
 * and the user interface.
 *
 * @author Angel Sanadinov
 */
public abstract class ResponseBean implements Serializable, SelfValidating
{
    private boolean requestResult;  //the result of the request
    private ApplicationException exception; //the response exception, if available

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public ResponseBean()
    {
        this.requestResult = false;
        this.exception = null;
    }

    /**
     * Constructs the response object using the supplied data.
     *
     * @param requestResult the result of the request. Set to <code>true</code> for
     *                      success or to <code>false</code> for failure
     * @param exception an exception object, if available
     */
    public ResponseBean(boolean requestResult, ApplicationException exception)
    {
        this.requestResult = requestResult;
        this.exception = exception;
    }

    /**
     * Retrieves the request result.
     *
     * @return <code>true</code> if the request associated with this response was
     *         successful or <code>false</code> otherwise
     */
    public boolean getRequestResult()
    {
        return requestResult;
    }

    /**
     * Retrieves the exception that occurred during the processing of the request.
     *
     * @return the exception or <code>null</code> if it is not available
     */
    public ApplicationException getException()
    {
        return exception;
    }

    /**
     * Retrieves a list of all available exceptions. <br><br>
     *
     * <b>Note:</b> <i>Should be fully implemented only when necessary, otherwise
     *              just return <code>null</code>.</i>
     *
     * @return the exceptions list or <code>null</code> if none are available
     */
    public abstract ArrayList<ApplicationException> getExceptions();

    /**
     * Checks the validity of the object. <br><br>
     *
     * <b>Note:</b> <i>For response objects this method always returns <code>true</code>.
     *              The validity of this object is irrelevant as there is no action
     *              that can be taken to solve potential issues.</i>
     *
     * @return <code>true</code>
     */
    @Override
    public boolean isValid()
    {
        return true;
    }

    /**
     * Returns a textual representation of the object. <br><br>
     *
     * It is in the form: "requestResult,[exception.toString()|null],[data_for_all_exceptions|null]".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        //checks if multiple exceptions are available and builds the required string
        StringBuilder exceptionsStrings = null;
        if(getExceptions() != null && !getExceptions().isEmpty())
        {
            exceptionsStrings = new StringBuilder("Exceptions (" + getExceptions().size() + "):");
            for(ApplicationException applicationException : getExceptions())
            {
                exceptionsStrings.append("(");
                exceptionsStrings.append(applicationException.toString());
                exceptionsStrings.append(");");
            }
        }
        else
            ;
        
        return requestResult + "," + ((exception != null) ? exception.toString() : null)
               + "," + ((exceptionsStrings != null) ? exceptionsStrings.toString() : null);
    }
}
